//
//  MapViewController.m
//  tradersOnMap
//
//  Created by bilmuh bilmuh on 08.06.2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MapViewController.h"
#import "Item.h"

@implementation MapViewController

-(void)setup
{
	UIImage *image = [UIImage imageNamed:@"mapMarker.png"];
	UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Map" image:image tag:0];
	self.tabBarItem = item;
	[item release], [image release];
}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
	self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil];
	if (self) {
		[self setup];
	}
	return self;
}

-(void)awakeFromNib
{
	[self setup];
}

-(void) viewDidLoad
{
	[super viewDidLoad];
	
	NSData* xmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://sorubank.ege.edu.tr/~b05067683/tez/getMapItems.php?latitude=38.45&longitude=27.15"]]];
    NSError* error;
	GDataXMLDocument* doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    NSArray *itemNodes = [doc nodesForXPath:@"//items/item" error:nil];
	NSMutableArray* items = [[NSMutableArray alloc] init];
	
	for (GDataXMLNode *itemNode in itemNodes)
	{
		Item* item = [[Item alloc] init];
		
		item.imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", ((GDataXMLElement*) [[itemNode nodesForXPath:@"imageURL" error:nil] objectAtIndex:0]).stringValue]];
		item.price = ((GDataXMLElement*) [[itemNode nodesForXPath:@"price" error:nil] objectAtIndex:0]).stringValue;
		item.itemTitle = ((GDataXMLElement*) [[itemNode nodesForXPath:@"itemTitle" error:nil] objectAtIndex:0]).stringValue;
		item.description = ((GDataXMLElement*) [[itemNode nodesForXPath:@"description" error:nil] objectAtIndex:0]).stringValue;
		item.latitude = ((GDataXMLElement*) [[itemNode nodesForXPath:@"latitude" error:nil] objectAtIndex:0]).stringValue;
		item.longitude = ((GDataXMLElement*) [[itemNode nodesForXPath:@"longitude" error:nil] objectAtIndex:0]).stringValue;
		
		[items addObject:item];
		
		[item release];
	}
	
	[mapView addAnnotations:items];	
	
	CLLocationCoordinate2D center;
	
	center.latitude = 38.45;
	center.longitude = 27.15;
	
	MKCoordinateRegion region = MKCoordinateRegionMake(center, MKCoordinateSpanMake(0.2, 0.2));
	
	[mapView setRegion:region];
}

/*-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
	MKAnnotationView* av = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"] autorelease];

	av.canShowCallout = YES;
	
	UIButton* button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
	av.rightCalloutAccessoryView = button;
	
	return av;
}*/

@end
